<?php
/**
 *
 * @author andrew carlson <andrew@acstudiosllc.com>
 * @version 0.0.1
 * @copyright 2012 AcStudios LLC
 * @package default
 */

namespace Encompass\Accounts;
USE PDO;

/**
 * class account functions
 *
 * pulled example code in to see if I can get this working
 *
 * @package default
 * @author  andrew carlson <andrew@acstudiosllc.com>
 */
class accountsController extends \Encompass\Core\coreController {

	function __construct() {
		// if an action is present, we need to route to there
		$action = \Encompass\Utilities\utilities::actionRoute();
		$this->$action();
	}

	function grid() {
		$loader = new \Twig_Loader_Filesystem('templates/');

		if (CACHING) {
			$twig = new \Twig_Environment($loader, array('cache' => 'tmp/cache'));
		} else {
			$twig = new \Twig_Environment($loader);
		}

		$template = $twig -> loadTemplate('accounts.tpl');

		$template -> display(array('pageTitle' => 'Encompass Accounts', ));
	}
	
	/**
	 * add a new user
	 * most of this will be moved to a model soon, just testing theories now
	 */
	function add_user() {
		// these need to be trapped, either here or through a utility function?
		$formFields = array(
			$_POST['first_name'],
			$_POST['last_name'],
			$_POST['email'],
			$_POST['role'],
		);

		// should call server side validation here
		
		// hand off to model to insert data
		$ac_model = new \Encompass\Accounts\accountsModel;
		$status = $ac_model->addUser($formFields);
		
		$ac_view = new \Encompass\Accounts\accountsView;
		$ac_view->returnStatus($status);
	}
	
	/**
	 * edit an existing user
	 */
	function edit_user() {
		
	}

	/**
	 * delete a user
	 */
	function del_user() {
		
	}
	
	/**
	 * simple list of users, may modify/tweak later
	 */
	function list_users() {
		$ac_model = new \Encompass\Accounts\accountsModel;
		$users = $ac_model->list_users(); // may need to pass grid values like position, sorting, etc

		$ac_view = new \Encompass\Accounts\accountsView;
		$ac_view->dhtmlx_json($users);
	}

}
